import { Injectable } from '@nestjs/common';
import Keyv from 'keyv';
import KeyvRedis from "@keyv/redis";
import { redisConfig } from 'src/utils/config';
@Injectable()
export class RedisService {
    private readonly keyv: Keyv;
    constructor() {
        this.keyv = new Keyv({
            store: new KeyvRedis({
                url: `redis://${redisConfig.username}:${redisConfig.password}@${redisConfig.host}:${redisConfig.port}`, 
            }),
            namespace: 'default', // 可选：为你的键添加命名空间以避免冲突,
            ttl: 1000 * 60 * 60 * 24,//毫秒
            
        });
    }
    
    async get(key: string): Promise<any> {
        const k = await this.keyv.get(key);
        return k
    }
    
    async set(key: string, value: any, ttl?: number): Promise<any> {
        return this.keyv.set(key, value, ttl);
    }

    async del(key: string): Promise<any> {
        return this.keyv.delete(key);
    }

    async setNS(nameSpace: string): Promise<any> {
        this.keyv.namespace = nameSpace
        return this
    }
}
